﻿// --------------------------------
// <copyright file="SoundItem.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:38</date>

// <summary>
//
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if WINDOWS

using System.ComponentModel;
using FITClub.Episode1.Levels;
using System.Drawing.Design;

#endif

using System.Xml.Serialization;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;

namespace FITClub.TextureMenu
{
#if WINDOWS

    [Serializable]
#endif
    public class SoundItem : IDisposable
    {
        #region Public properties

        /// <summary>
        /// Path to content file.
        /// </summary>
        [XmlAttribute]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string Path { get; set; }

        /// <summary>
        /// Effect volume.
        /// </summary>
        [XmlAttribute]
        public float Volume { get; set; }

        /// <summary>
        ///
        /// </summary>
        [XmlAttribute]
        public float Pitch { get; set; }

        /// <summary>
        ///
        /// </summary>
        [XmlAttribute]
        public float Pan { get; set; }

        /// <summary>
        ///
        /// </summary>
        [XmlIgnore]
#if WINDOWS
        [Browsable(false)]
        [NonSerialized]
#endif
        private SoundEffect Effect;

        [XmlIgnore]
#if WINDOWS
        [Browsable(false)]
#endif
        public bool ContentLoaded { get; set; }

        #endregion Public properties

        public SoundItem()
        {
            this.Volume = 1;
        }

        public void LoadContent(ContentManager loader)
        {
            this.Effect = loader.Load<SoundEffect>(this.Path);
            this.ContentLoaded = true;
        }

        /// <summary>
        /// PLay sound effect.
        /// </summary>
        public void Play()
        {
            if (this.ContentLoaded)
            {
                this.Effect.Play(this.Volume, this.Pitch, this.Pan);
            }
        }

        public void Dispose()
        {
            try
            {
                this.Effect.Dispose();
            }
            catch
            {
            }
        }
    }
}